//
//  PMTableViewController.m
//  proxy
//
//  Created by lishunnian on 11-3-9.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PMTableViewController.h"
#import "PMProxy.h"

@implementation PMTableViewController

@synthesize proxies = proxies_;

- (id)init
{
    if (self=[super init])
    {
        proxies_ = [[NSMutableArray alloc] init];
        NSString *url = @"localhost";
        NSNumber *port = [NSNumber numberWithInt:80];
        PMProxyType type = kPMHttp;
        PMStatus status = kPMConnecting;
        PMProxy *proxy = [[[PMProxy alloc] init] autorelease];
        proxy.hostAddress = url;
        proxy.portNum = port;
        proxy.proxyType = type;
        proxy.status = status;
        [proxies_ addObject:proxy];
    }
    return self;
}

- (void)dealloc
{
    [proxies_ release];
    [super dealloc];
}
- (void)addObjectToProxis:(id)anObject
{
	[proxies_ addObject:anObject];
}

- (void)insertObject:(id)anObject inProxisAtIndex:(unsigned int)i 
{
	[proxies_ insertObject:anObject atIndex:i];
}

- (id)objectInProxisAtIndex:(unsigned int)i
{
	return [proxies_ objectAtIndex:i];
}

- (unsigned int)indexOfObjectInProxis:(id)anObject
{
	return [proxies_ indexOfObject:anObject];
}

- (void)removeObjectFromProxisAtIndex:(unsigned int)i
{
	[proxies_ removeObjectAtIndex:i];
}

- (unsigned int)countOfProxis
{
	return [proxies_ count];
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    PMProxy *aProxy = [proxies_ objectAtIndex:row];
    if (aProxy)
    {
        NSString *identifier = [tableColumn identifier];
        if ([identifier isEqualToString:@"proxyType"])
        {
            switch (aProxy.proxyType)
            {
                case kPMHttp:
                    return @"Http";
                    break;
                case kPMSocket4:
                    return @"Sock4";
                    break;
                case kPMSocket5:
                    return @"Sock5";
                    break;
                default:
                    return @"UnKnow";
                    break;
            }
        }
        if ([identifier isEqualToString:@"status"])
        {
            switch (aProxy.status)
            {
                case kPMConnecting:
                    return @"Connecting...";
                    break;
                case kPMNotCheck:
                    return @"";
                    break;
                case kPMSuccess:
                    return [NSString stringWithFormat:@"%f", aProxy.checkTime];
                    break;
                case kPMConnectError:
                    return @"Connect Error!";
                    break;
                case kPMTimeOut:
                    return @"Connect Timeout!";
                    break;
                default:
                    return @"";
                    break;
            }
        }
        return [aProxy valueForKey:[tableColumn identifier]];
    }
    return nil;
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [proxies_ count];
}
@end
